import 'package:dio/dio.dart';

// 根据userId获取UserDetail
Future<Response<dynamic>> getUserDetailByUserId({
  required int userId,
  required Dio dio,
}) async {
  Response<dynamic> response = await dio.get('/userdetail/$userId');
  return response;
}

// 添加UserDetail
Future<Response<dynamic>> addUserDetailByUserId({
  required int userId,
  required Map<String, dynamic> userDetailData,
  required Dio dio,
}) async {
  Response<dynamic> response = await dio.post(
    '/userdetail/$userId',
    data: userDetailData,
  );
  return response;
}

// 根据userId删除UserDetail
Future<Response<dynamic>> deleteUserDetailByUserId({
  required int userId,
  required Dio dio,
}) async {
  Response<dynamic> response = await dio.delete('/userdetail/$userId');
  return response;
}

// 更新UserDetail
Future<Response<dynamic>> updateUserDetailByUserId({
  required int userId,
  String? bio,
  String? imagePath,
  int? followersCount,
  int? followingCount,
  String? gender,
  String? birthday,
  String? location,
  String? school,
  required Dio dio,
}) async {
  final Map<String, dynamic> formDataMap = {};
  if (bio != null) {
    formDataMap['bio'] = bio;
  }
  if (imagePath != null && imagePath.isNotEmpty) {
    formDataMap['imageFile'] = await MultipartFile.fromFile(
      imagePath,
      filename: imagePath.split('/').last,
    );
  }
  if (followersCount != null) {
    formDataMap['followersCount'] = followersCount;
  }
  if (followingCount != null) {
    formDataMap['followingCount'] = followingCount;
  }
  if (gender != null) {
    formDataMap['gender'] = gender;
  }
  if (birthday != null) {
    formDataMap['birthday'] = birthday;
  }
  if (location != null) {
    formDataMap['location'] = location;
  }
  if (school != null) {
    formDataMap['school'] = school;
  }

  FormData formData = FormData.fromMap(formDataMap);

  try {
    Response<dynamic> response = await dio.put(
      '/userdetail/$userId',
      data: formData,
    );
    print(response);
    return response;
  } catch (e) {
    print('Error: $e');
    throw e;
  }
}
